package com.wu.util;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * LocalPushMsgTempateContentEnum 本地push消息模板内容
 *
 * @author wangying
 * @version 2024/09/07 17:57
 **/
public class LocalPushMsgTemplateContent {


    /**
     * 本地push消息模板内容父级属性枚举
     */
    @Getter
    @AllArgsConstructor
    public enum LocalPushMsgTempateContentParentEnum {

        BASE("baseRes", "基础资源"),
        RECOMMEND("recommendRes", "推荐资源");

        private final String name;

        private final String desc;

        public static Boolean contains(String content){
            for (LocalPushMsgTempateContentParentEnum parentEnum : LocalPushMsgTempateContentParentEnum.values()) {
                if (parentEnum.getName().equals(content)){
                    return Boolean.TRUE;
                }
            }
            return Boolean.FALSE;
        }
    }

    /**
     * 本地push消息模板内容子级属性枚举
     */
    @Getter
    @AllArgsConstructor
    public enum LocalPushMsgTempateContentChildEnum {

        SONG_ID("songId","歌曲ID"),
        ACTOR_ID("actorId","歌手ID"),
        ALBUM_ID("albumId","专辑ID"),
        PLAYLIST_ID("playlistId","歌单ID"),
        SONG_NAME("songName","歌曲名称"),
        ACTOR_NAME("actorName","歌手名称"),
        ALBUM_NAME("albumName","专辑名称"),
        PLAYLIST_NAME("playlistName","歌单名称");


        private final String name;

        private final String desc;

        public static Boolean contains(String content){
            for (LocalPushMsgTempateContentChildEnum childEnum : LocalPushMsgTempateContentChildEnum.values()) {
                if (childEnum.getName().equals(content)){
                    return Boolean.TRUE;
                }
            }
            return Boolean.FALSE;
        }
    }


    /**
     * 校验属性字段结构
     * @param content
     * eg:baseRes.songName
     * @return
     */
    public static Boolean checkAttributeStructure(String content){
        if (StringUtils.isNotBlank(content)){
            List<String> contents = List.of(content.split("\\."));
            if (contents.size()==2){
                if (LocalPushMsgTempateContentParentEnum.contains(contents.get(0))&&LocalPushMsgTempateContentChildEnum.contains(contents.get(1))){
                    return Boolean.TRUE;
                }
            }
        }
        return Boolean.FALSE;
    }

    /**
     * 获取属性字段
     * @param content
     * @return
     */
    public static List<String> getAttribute(String content){
        List<String> attributes = new ArrayList<>();

        // 正则表达式匹配 {} 中的内容
        String regex = "\\{([a-zA-Z.]+)\\}";
        Pattern pattern = Pattern.compile(regex);

        // 创建 Matcher 对象
        Matcher matcher = pattern.matcher(content);

        // 使用 while 循环遍历所有匹配项
        while (matcher.find()) {
            String fieldName = matcher.group(1); // 提取 {} 中的内容
            attributes.add(fieldName);
        }
        return attributes;
    }
}